#include "wuFilmXMLShape.h"

wuFilmXMLShape::wuFilmXMLShape()
{
}

wuFilmXMLShape::~wuFilmXMLShape()
{
}


void wuFilmXMLShape::setup( string _filename, ofxBox2d * _box2d, int _vw, int _vh, vector <wuSkin> * _skins, bool _bAuto)
{
    wuFilmXML::setup( _filename, _skins, _bAuto);
    vw=_vw;
    vh=_vh;
    box2d = _box2d;

	// set texture-mix
	myFBO.allocate(vw, vh, GL_RGBA);
	myFBO.clear(0, 0, 0, 0);

	grayDiff.clear();
	grayDiff.allocate(vw, vh);

    bShape = true;
}

void wuFilmXMLShape::update()
{
    wuFilmXML::update();
    if(bShape)
    {
        makeFBO();
        FindShape();
    }
}

void wuFilmXMLShape::draw(bool bDrawlines, int _offX)
{
    wuFilmXML::draw(bDrawlines, _offX);

    if(bDrawlines && _offX!=0)
    {
        for(int i=0; i< polyLines.size(); i++)
            polyLines[i].draw();
    }
}

void wuFilmXMLShape::makeFBO()
{

	myFBO.clear(0, 0, 0, 0);
    myFBO.begin();
            for(int i=0; i < frame.size(); i++)
            {
                float ang = frame[i].ang;
                ofPoint pos = frame[i].pos;

                int j=i%(skins->size());

                // draw Skin at position
                ofPushMatrix();
                    ofTranslate(pos.x, pos.y);
                    (*skins)[j].skin.setAnchorPercent(0.5, 0.5);
                    ofRotateZ(ofRadToDeg(ang));
                    ofEnableAlphaBlending();
                    (*skins)[j].skin.draw(0, 0);
                    ofDisableAlphaBlending();
                ofPopMatrix();
            }
    myFBO.end();

	grayDiff.setFromAlphaPixels((unsigned char *)myFBO.getPixels(), vw, vh, true);
	grayDiff.blur(5);

}

void wuFilmXMLShape::FindShape()
{
		    contour.blobs.clear();
			contour.findContours(grayDiff, 20, (vw*vh), 10, false);	// find holes

            for(int i=0; i< polyLines.size(); i++)
                polyLines[i].destroy();

            polyLines.clear();
//            polyLine.destroy();

				// rectangle al voltant del bixo
                rect.x = contour.blobs[0].boundingRect.x+framePos.x;
                rect.y = contour.blobs[0].boundingRect.y+framePos.y;
                rect.width = contour.blobs[0].boundingRect.width;
                rect.height = contour.blobs[0].boundingRect.height;

            for(int i=0; i<contour.nBlobs; i++){


                // make the shape
               if(contour.blobs[i].pts.size()>2)
                {
                   ofxBox2dPolygon polyLine;

                   for(int j=contour.blobs[i].pts.size()/2; j<contour.blobs[i].pts.size(); j++) {
                        polyLine.addVertex(contour.blobs[i].pts[j].x+framePos.x, contour.blobs[i].pts[j].y+framePos.y);
                   }
                   for(int j=0; j<contour.blobs[i].pts.size(); j++) {
                        polyLine.addVertex(contour.blobs[i].pts[j].x+framePos.x, contour.blobs[i].pts[j].y+framePos.y);
                   }
                   if(polyLine.isGoodShape())
                   {
                       // at least 3 points (treure el simplify si es queixa)
                       polyLine.simplify(5.0);
                       polyLine.setPhysics(0.0, 0.5, 0.5);
                       polyLine.create(box2d->getWorld());
                       polyLines.push_back(polyLine);
                   }
               }
			}
}

void wuFilmXMLShape::setNowScrollOffset(int _scrollOff)
{
    scrollNowOffset = _scrollOff;
}

void wuFilmXMLShape::clear()
{
    for(int i=0; i< polyLines.size(); i++)
        polyLines[i].destroy();
    wuFilmXML::clear();
}

ofPoint wuFilmXMLShape::getPosition()
{
    ofPoint _pos;

    _pos.x = rect.x;
    _pos.y = rect.y;

	return _pos;
}
